<?php
	/**
	 * 问题管理控制器
	 */
	class AskController extends AuthController{
		public function index(){
			
			//返回所有的提问
			$this->getAllask();
			$this->display();
		}
		/**
		 * 删除指定的提问
		 */
		public function del(){
			$asid = $_GET['asid'];
			$model = M('ask');
			//查询相应提问的UID
			$uid = current($model->query("SELECT uid FROM hd_ask WHERE asid={$asid}"))['uid'];
			$result = $model->_exec("DELETE FROM hd_ask WHERE asid={$asid}");
			//提问被删除扣除的金币数
			$delask = $this->getRule()['DELASK'];			
			//删除相应的金币数
			$dels = $model->_exec("UPDATE hd_user SET point=point-{$delask} WHERE uid={$uid}");
			//删除所有相应的回答
			$result = $model->_exec("DELETE FROM hd_answer WHERE asid={$asid}");
			$this->success('提问删除成功~!',__APP__.'?c=ask');
			
		}
		
		
		/**
		 * 返回所有的提问
		 */
		private function getAllask(){
			$model = M('ask');
			$result = $model->query("SELECT a.time,a.asid,a.uid,a.content,a.answer,a.reward,u.username FROM hd_ask as a JOIN hd_user as u ON a.uid=u.uid ORDER BY time DESC");
			$this->assign('result',$result);
		}
		
		/**
		 * 返回所有未解决的问题
		 */
		 public function getnosolve(){
		 	$model = M('ask');
			$result = $model->query("SELECT a.time,a.asid,a.uid,a.content,a.answer,a.reward,u.username FROM hd_ask as a JOIN hd_user as u ON a.uid=u.uid AND a.solve=0 ORDER BY a.time DESC");
			$this->assign('result',$result);
			$this->display();
		 }
		 /**
		 * 返回所有已解决的问题
		 */
		 public function solveed(){
		 	$model = M('ask');
			$result = $model->query("SELECT a.time,a.asid,a.uid,a.content,a.answer,a.reward,u.username FROM hd_ask as a JOIN hd_user as u ON a.uid=u.uid   WHERE a.solve=1 ORDER BY time DESC");
			$this->assign('result',$result);
			$this->display();
		 }
		 
		 /**
		  * 返回无人回答的提问
		  */
		  public function noanswer(){
		 	$model = M('ask');
			$result = $model->query("SELECT a.time,a.asid,a.uid,a.content,a.answer,a.reward,u.username FROM hd_ask as a JOIN hd_user as u ON a.uid=u.uid  WHERE a.solve=0 AND a.answer=0 ORDER BY a.time DESC");
			$this->assign('result',$result);
			$this->display();
		 }
				
		
	}
